import os


def is_rect_intersect(r1,r2):
    """ format of r1 and r2 is (xmin,ymin,xmax,ymax) """
    
    # a is left of b || a is right of b || a is above b || a is below b
    if (r1[2] < r2[0]) or (r1[0] > r2[2]) or (r1[3] < r2[1]) or (r1[1] > r2[3]):        
        return False
    
    return True
    
    
def area_rect_intersect(r1,r2):
    """ format of r1 and r2 is (xmin,ymin,xmax,ymax) """
    
    tl = ( max(r1[0],r2[0]), max(r1[1],r2[1]) )
    br = ( min(r1[2],r2[2]), min(r1[3],r2[3]) )
    
    w = br[0] - tl[0]
    h = br[1] - tl[1]
    
    if w <= 0 or h <= 0:
        return False
    
    return w*h
    
    
    
if __name__ == "__main__":
    
    path = '/Users/tpilanta/Documents/workspace/python/ua-bb-model-framework-2013'
    polfile = os.path.join(path,'pol.txt')
    outdict = {}
    with open(polfile, 'r') as f:
        content = f.readlines()
        previd = -1
        prev = ()
#        count = 0
        for row in content:
            row = row.strip().split(" ")
            actorid = row[0]
            xmin = row[1]
            ymin = row[2]
            xmax = row[3]
            ymax = row[4]
            frame = row[5]
            valid = row[6]  # if 0, then valid
            label = row[9]
            
            if int(valid) == 1:
                continue
                
            if int(previd) == int(actorid): 
                prev = (actorid,xmin,ymin,xmax,ymax,frame,label)
                continue 
            else:
                if outdict.has_key(previd):
                    outdict[previd].append(prev)
#                if count > 260561:
#                    break
#                count += 1
                outdict[actorid] = [(actorid,xmin,ymin,xmax,ymax,frame,label)]
                prev = (actorid,xmin,ymin,xmax,ymax,frame,label)
                previd = actorid
        
        
        if outdict.has_key(previd):
            outdict[previd].append(prev)
    
    keys = outdict.keys()
    for i in range(0,len(keys)):
        for j in range(i+1,len(keys)):
            # compute person id keys[i] with person id keys[j]
            rowi = outdict[keys[i]]
            rowj = outdict[keys[j]]
#            print keys[i], keys[j] 
#            print rowi, rowj
            starti = rowi[0]
            endi = rowi[1]
            startj = rowj[0]
            endj = rowj[1]
                
            frthreshold = 5
            
            # check if end frame is less than start frame, specifically 5 frames
            if int(endi[5]) < int(startj[5]) and abs(int(startj[5]) - int(endi[5])) < frthreshold:
                # (xmin,ymin,xmax,ymax)
                r1 = (int(endi[1]),int(endi[2]),int(endi[3]),int(endi[4]))          
                r2 = (int(startj[1]),int(startj[2]),int(startj[3]),int(startj[4]))  
                
                if is_rect_intersect(r1,r2):
                    area = area_rect_intersect(r1,r2)
                    print "ID:", endi[0], "Frame:", endi[5], "    ID:", startj[0], "Frame:", startj[5]
                    print "Intersect area:", area
            
            elif int(endj[5]) < int(starti[5]) and abs(int(starti[5]) - int(endj[5])) < frthreshold:
                # (xmin,ymin,xmax,ymax)
                r1 = (int(endj[1]),int(endj[2]),int(endj[3]),int(endj[4]))          
                r2 = (int(starti[1]),int(starti[2]),int(starti[3]),int(starti[4])) 
                
                if is_rect_intersect(r1,r2):
                    area = area_rect_intersect(r1,r2)
                    print "ID:", endj[0], "Frame:", endj[5], "    ID:", starti[0], "Frame:", starti[5]
                    print "Intersect area:", area
                    
